package com.xsb.customview.progressbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 水平加载进度条
 * */
public class RectangleProgressView extends View {
    /*画笔*/
    private Paint mPaint;
    /*控件宽度*/
    private int mWidth;
    /*控件高度*/
    private int mHeight;
    /*当前进度*/
    private float currentCount;
    /*最大进度*/
    private float maxCount;

    public RectangleProgressView(Context context) {
        super(context);
    }

    public RectangleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RectangleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /*设置当前进度*/
    public void setCurrentCount(float currentCount) {
        this.currentCount = currentCount > maxCount ? maxCount : currentCount;
        /**
         * invalidate()是用来刷新View的，必须是在UI线程中进行工作。比如在修改某个view的显示时，
         * 调用invalidate()才能看到重新绘制的界面。invalidate()的调用是把之前的旧的view从主UI
         * 线程队列中pop掉。
         */
        invalidate();
    }

    /*设置最大进度*/
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }

    /*得到当前最大进度*/
    public float getMaxCount() {
        return maxCount;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint = new Paint();
        //设置抗锯齿效果
        mPaint.setAntiAlias(true);
        //计算椭圆圆角角度
        int round = mHeight/2;

        //设置最底层圆角矩形
        mPaint.setColor(Color.BLACK);
        RectF rfOne = new RectF(0,0,mWidth,mHeight);
        canvas.drawRoundRect(rfOne,round,round,mPaint);

        //设置第二层圆角矩形
        mPaint.setColor(Color.GRAY);
        RectF rfTwo = new RectF(2,2,mWidth-2,mHeight-2);
        canvas.drawRoundRect(rfTwo,round,round,mPaint);

        //设置第三层圆角矩形
        float ppt = currentCount/maxCount;
        mPaint.setColor(Color.GREEN);
        RectF rfThree = new RectF(3,3,(mWidth-3)*ppt,mHeight-3);
        canvas.drawRoundRect(rfThree,round,round,mPaint);

        //设置百分比文字
        mPaint.reset();
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(dipToPx(15));
        canvas.drawText((int)(ppt*100)+"%",mWidth/2,mHeight/2-(mPaint.ascent()+mPaint.descent())/2,mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        /*MeasureSpec.EXACTLY，精确尺寸 ;MeasureSpec.AT_MOST，最大尺寸，只要不超过父控件允许的最大尺寸即可
        *
        * MeasureSpec.UNSPECIFIED未指定
        * */
        if(widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST){
            mWidth = widthSize;
        }else{
            mWidth = 0;
        }

        if(heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED){
            mHeight = dipToPx(20);
        }else{
            mHeight = heightSize;
        }
        setMeasuredDimension(mWidth,mHeight);
    }

    //dip * scale + 0.5f * (dip >= 0 ? 1 : -1)
    private int dipToPx(int dip){
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));//加0.5是为了四舍五入
    }
}
